#include "CommandManager.h"
#include "../CommandNotFoundException/CommandNotFoundException.h"

CommandManager::CommandManager() {
    //do nothing
}

CommandManager::CommandManager(int command_size) {
    commands.clear();
    commands.resize(command_size);
}

CommandManager::CommandManager(CommandManager& _cm) {
    this->commands = _cm.commands;
}

CommandManager::CommandManager(CalendarManager& _cal_manager) {
    this->cal_manager = _cal_manager;
}

CommandManager::~CommandManager() {
    this->commands.clear();
}

bool CommandManager::IsCommandExist(char* com) {
    int i;
    for (i = 0; i < commands.size(); i++) {
        if (!strcmp(commands[i].GetName().c_str(), com)) {
            return true;
        }
    }
    return false;
}

CalendarManager& CommandManager::GetCalendarManager() {
    return this->cal_manager;
}

void CommandManager::AddCommand(const Command& _command) {
    this->commands.push_back(_command);
}

void CommandManager::Execute(long entryNo, const string& _command_name, char** argv) {
    for (int i = 0; i < commands.size(); ++i) {
        if (commands[i].GetName() == _command_name) {
            if (cal_manager.IsExistCalendar(entryNo)) {
                commands[i].Execute(cal_manager.GetCalendar(entryNo), argv);
            } else {
                //throw CommandNotFoundException("Command Not Found");
                cout << "Command Not Found" << endl;
            }
        }
    }
}
